<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { getArticlesList, getChannelsList } from '@/services/home'
import type { Articles, ChannelsList } from '@/types/home'
import { format } from 'timeago.js'

const active = ref(0)
const channelsList = ref<ChannelsList>()

const loadChannelsList = async () => {
  const res = await getChannelsList()
  channelsList.value = res.data
}

const articleList = ref<Articles>([])

onMounted(() => {
  loadChannelsList()
})

const loading = ref(false)
const finished = ref(false)
const refreshing = ref(false)
const timestamp = ref('')
timestamp.value = String(Date.now())
const onLoad = async () => {
  if (refreshing.value) {
    articleList.value = []
    refreshing.value = false
  }

  const { data } = await getArticlesList(
    String(channelsList.value?.channels[active.value].id),
    timestamp.value
  )
  articleList.value.push(...data.results)
  if (data.pre_timestamp) {
    timestamp.value = data.pre_timestamp
  } else {
    finished.value = true
  }
  loading.value = false
}
const onRefresh = () => {
  articleList.value = []
  finished.value = false
  loading.value = true
  onLoad()
}
const onClickTab = () => {
  articleList.value = []
  finished.value = false
  loading.value = true
  onLoad()
}
</script>

<template>
  <div class="home-page">
    <van-nav-bar fixed>
      <template #left>
        <div class="logo">
          <ic-icon name="logo"></ic-icon>
        </div>
      </template>
      <template #right>
        <div class="search">
          <ic-icon name="home-search"></ic-icon>
        </div>
      </template>
    </van-nav-bar>
    <van-tabs
      v-model:active="active"
      shrink
      sticky
      style="top: 11.73vw"
      offset-top="11.73vw"
      @click-tab="onClickTab"
    >
      <van-tab
        v-for="item in channelsList?.channels"
        :key="item.id"
        :title="item.name"
      >
        <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
          <van-list
            v-model:loading="loading"
            :finished="finished"
            finished-text="没有更多了"
            @load="onLoad"
          >
            <div
              class="article"
              v-for="item in articleList"
              :key="item.art_id"
              @click="$router.push(`/article/?id=${item.art_id}`)"
            >
              <van-row v-if="item.cover.type === 1">
                <van-col span="16">
                  <div class="title">{{ item.title }}</div>
                  <div class="footer">
                    {{ item.aut_name }}
                    &nbsp;
                    {{
                      format(item.pubdate, 'zh_CN')
                        .split(/[\t\r\f\n\s]*/g)
                        .join('')
                    }}
                  </div>
                </van-col>
                <van-col span="8">
                  <van-image
                    v-for="img in item.cover.images"
                    :key="img"
                    :src="img"
                  />
                </van-col>
              </van-row>
              <div class="title">{{ item.title }}</div>
              <van-row v-if="item.cover.type === 2 || item.cover.type === 3">
                <van-col span="8" v-for="img in item.cover.images" :key="img">
                  <van-image :src="img" />
                </van-col>
              </van-row>
              <div class="footer">
                {{ item.aut_name }}
                &nbsp;
                {{
                  format(item.pubdate, 'zh_CN')
                    .split(/[\t\r\f\n\s]*/g)
                    .join('')
                }}
              </div>
            </div>
          </van-list>
        </van-pull-refresh>
      </van-tab>
    </van-tabs>
    <div class="placeholder"></div>
  </div>
</template>

<style lang="scss" scoped>
::v-deep(.van-nav-bar__content) {
  height: 44px;
}
::v-deep(.van-image__img, .van-image__error, .van-image__loading) {
  width: 105px;
  height: 68px;
}
::v-deep(.van-tab) {
  font-size: 17px;
  color: var(--ic-color1);
  line-height: 23px;
}
:deep() {
  .van-tabs {
    &__line {
      width: 17px;
      height: 4px;
      background-color: --ic-color2;
    }
    &__wrap {
      height: 63px;
    }
  }
}
::v-deep(
    .van-tabs__nav--line.van-tabs__nav--shrink,
    .van-tabs__nav--line.van-tabs__nav--complete
  ) {
  padding-left: 16px;
}
.logo {
  width: 80px;
  height: 26px;
}
.search {
  width: 22px;
  height: 22px;
}
.article {
  margin: 12px 0;
  padding: 0 24px;
  .footer {
    margin-top: 8px;
    font-size: 12px;
    color: var(--ic-color3);
    height: 20px;
    line-height: 20px;
  }
}
.placeholder {
  height: 48px;
}
</style>
